package com.example.rfidserver.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.ListAdapter;

import com.example.rfidserver.pojo.Ticket;

import java.util.ArrayList;
import java.util.List;

public class TicketDao {


    public List<Ticket> getTicketInfo() {
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db", null, SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = db.query("ticket", null, null, null, null, null, null);
        List tickets = new ArrayList();
        while (cursor.moveToNext()) {
            int ticket_id = cursor.getInt(0);
            String ticket_type = cursor.getString(1);
            String ticket_description = cursor.getString(2);
            Ticket ticket = new Ticket();
            ticket.setTicket_id(ticket_id);
            ticket.setTicket_type(ticket_type);
            ticket.setTicket_description(ticket_description);
            tickets.add(ticket);
        }
        db.close();
        return tickets;
    }


    public int addOneTicket(String ticket_type, String ticket_description) {
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db", null, SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        System.out.println("ticket_type " + ticket_type);
        cv.put("ticket_type", ticket_type);
        cv.put("ticket_description", ticket_description);
        int res = (int) db.insert("ticket", null, cv);
        System.out.println("res:" + res);
        db.close();
        return res;
    }

    public int deleteOneTicketByID(int ticketID) {
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db", null, SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        String[] args = {String.valueOf(ticketID)};
        int res = db.delete("ticket", "ticket_id=?", args);
        db.close();
        return res;
    }

    //传递过来一个ticket对象，我要更新它
    public int updateOneTicketByID(Ticket ticket) {
        int ticket_id = ticket.getTicket_id();
        //权限设置为可都可写
        SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.rfidserver/files/a.db", null, SQLiteDatabase.OPEN_READWRITE);
        ContentValues cv = new ContentValues();
        cv.put("ticket_type", ticket.getTicket_type());
        cv.put("ticket_description", ticket.getTicket_description());
        String[] args = {String.valueOf(ticket_id), String.valueOf(ticket.getTicket_type()), String.valueOf(ticket.getTicket_description())};
        int res = db.update("ticket", cv, "ticket_id=" + ticket_id, null);
        db.close();
        return res;

    }
}



